package bidgetserver;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import bidgetserver.Product;
import bidgetserver.PMF;

public class DeleteProduct extends HttpServlet {
    private static final Logger log = Logger.getLogger(AddUserProduct.class.getName());

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
        //UserService userService = UserServiceFactory.getUserService();
        //User user = userService.getCurrentUser();
        boolean flag = true;
        String itemToDel = req.getParameter("content2");
        String userName = req.getParameter("userName");
        if(itemToDel == null){
        	itemToDel = req.getParameter("content4");
        	flag= false;
        }

        PersistenceManager pm = PMF.get().getPersistenceManager();
        String query = "select from "+Product.class.getName()+" where author == '"+userName+"'";
        query+=" && ProductKey == '"+itemToDel+"'";
        if(flag)
        	query+=" && flag == true";
        else
        	query+=" && flag == false";
        List<Product> userProduct = (List<Product>) pm.newQuery(query).execute();

        try{
        	pm.deletePersistent(userProduct.get(0));
        }
        finally{
        	pm.close();
        }

    }
}
